package patrol.mobile.com.mobilepatrol.activity.culvert;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.Hdjcxjcb;
import patrol.mobile.com.mobilepatrol.listener.TimeListener;
import patrol.mobile.com.mobilepatrol.manager.HManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/9/27.
 */

public class CulvertOffenCheckDetailActivity extends BaseActivity {
//    @BindView(R.id.itme1)
//    RelativeLayout rl1;

    @BindView(R.id.itme2)
    RelativeLayout rl2;

    @BindView(R.id.itme3)
    RelativeLayout rl3;
    @BindView(R.id.itme4)
    RelativeLayout rl4;
    @BindView(R.id.itme5)
    RelativeLayout rl5;
    @BindView(R.id.itme6)
    RelativeLayout rl6;
    @BindView(R.id.itme7)
    RelativeLayout rl7;
    @BindView(R.id.itme8)
    RelativeLayout rl8;
    @BindView(R.id.itme9)
    RelativeLayout rl9;


    @BindView(R.id.bt_save)
    Button bt_save;

    @BindView(R.id.bt_list)
    Button bt_list;

//    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText et4;
    private EditText et5;
    private EditText et6;
    private TextView et7;
    private EditText et8;
    private EditText et9;

    private Hdjcxjcb hJC;
    private String hdjcxjcid;
    private String hdcode;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_culvert_offen_check_detail;
    }

    @Override
    public void setListener() {
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                final String id = et1.getText().toString().trim();
                final String roadcode = et2.getText().toString().trim();
                final String roadname = et3.getText().toString().trim();
                final String hdcode = et4.getText().toString().trim();
                final String roadpos = et5.getText().toString().trim();
                final String lx = et6.getText().toString().trim();
                final String jcsj = et7.getText().toString().trim();
                final String fzr = et8.getText().toString().trim();
                final String jlr = et9.getText().toString().trim();


                PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        RealmResults<Hdjcxjcb> br = realm.where(Hdjcxjcb.class).equalTo("id",hdjcxjcid).findAll();
                        Hdjcxjcb b = br.get(0);
//                        b.id = id;
                        b.roadcode = roadcode;
                        b.roadname = roadname;
                        b.hdcode = hdcode;
                        b.roadpos = Double.valueOf(roadpos);
                        b.lx = lx;
                        b.jcsj = jcsj;

                        b.jlr = jlr;
                        b.fzr = fzr;
                    }
                });
                HManager.setHDJCUpState(hdjcxjcid,false);
                HManager.setHDUpState(hdcode,false);
                showToast("保存成功");
            }
        });
        bt_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查桥梁id，定期检查id
                Intent intent = new Intent(CulvertOffenCheckDetailActivity.this,CulvertOffenCheckBrokenListActivity.class);
                intent.putExtra("hdjcxjcid",hdjcxjcid);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertOffenCheckDetailActivity.this,false);
            }
        });
//        tv11.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CommonUtil.showDatePickDlg(CulvertOffenCheckDetailActivity.this, new TimeListener() {
//                    @Override
//                    public void getDate(String date) {
//                        tv11.setText(date);
//                    }
//                });
//            }
//        });
        rl7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(CulvertOffenCheckDetailActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        et7.setText(date);
                    }
                });
            }
        });
    }

    @Override
    public void initView() {
        super.initView();
//        TextView tv1 = rl1.findViewById(tv);
//        tv1.setText("涵洞经常性检查id");
//        et1 = rl1.findViewById(et);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("路线编码");
        et2 = rl2.findViewById(et);

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("路线名称");
        et3 = rl3.findViewById(et);

        TextView tv4 = rl4.findViewById(tv);
        tv4.setText("涵洞编号");
        et4 = rl4.findViewById(et);

        TextView tv5 = rl5.findViewById(tv);
        tv5.setText("涵洞桩号");
        et5 = rl5.findViewById(et);

        TextView tv6 = rl6.findViewById(tv);
        tv6.setText("类型");
        et6 = rl6.findViewById(et);

        TextView tv7 = rl7.findViewById(tv);
        tv7.setText("检查时间");
        et7 = rl7.findViewById(R.id.tv1);

        TextView tv8 = rl8.findViewById(tv);
        tv8.setText("负责人");
        et8 = rl8.findViewById(et);

        TextView tv9 = rl9.findViewById(tv);
        tv9.setText("记录人");
        et9 = rl9.findViewById(et);




    }

    @Override
    public void initData() {
        super.initData();
        hdjcxjcid = getIntent().getStringExtra("hdjcxjcid");
        hdcode = getIntent().getStringExtra("hdcode");
        hJC = HManager.getCulvertJC(hdcode,hdjcxjcid);

        setData();

    }

    private void setData() {
//        et1.setText(hJC.id);

        et2.setText(hJC.roadcode);

        et3.setText(hJC.roadname);

        et4.setText(hJC.hdcode+"");

        et5.setText(hJC.roadpos+"");

        et6.setText(hJC.lx);

        et7.setText(hJC.jcsj);

        et8.setText(hJC.fzr);

        et9.setText(hJC.jlr+"");

    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(CulvertOffenCheckDetailActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("涵洞经常检查");
    }
}
